let logger = require("pomelo-logger").getLogger(__filename);
const ACTIVITY_COLLECTION_NAME = "activity";

let Dao = function (app) {
    this.app = app;
};

Dao.prototype.findById = function (id, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ACTIVITY_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`find activity failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.findOne({id : id}, function (error, document) {
            if(!!error) {
                logger.error(`findById in activities failed. error=${error}`);
                callback(null);
                return;
            }
            callback(document);
        });
    });
};

Dao.prototype.findAllActivities = function (callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ACTIVITY_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`find activity in activities failed. get collection error. error=${error}`);
            callback([]);
            return;
        }
        collection.find({}).toArray(function (error, documents) {
            if(error){
                logger.error(`findAllActivities in activities failed. error=${error}`);
                callback([]);
                return;
            }
            callback(documents);
        });
    });
};

Dao.prototype.insertOne = function (document, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ACTIVITY_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`insert activity in activities failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.insertOne(document, function (error, result) {
            if (!!error) {
                logger.error(`insert one activity in activities failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};

Dao.prototype.updateUrl = function (id, url, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ACTIVITY_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`update activity in activities failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.updateOne({id : id}, {$set:{url : url}}, function (error, result) {
            if (!!error) {
                logger.error(`update activity in activities failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};

Dao.prototype.updatePriority = function (id, priority, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ACTIVITY_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`update activity in activities failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.updateOne({id : id}, {$set:{priority : priority}}, function (error, result) {
            if (!!error) {
                logger.error(`update activity in activities failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};

Dao.prototype.update = function (id, title, priority, url, startTime, endTime, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ACTIVITY_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`update activity in activities failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.updateOne({id: id}, {$set:{title: title, priority: priority, url: url, startTime: startTime, endTime: endTime}},
        function (error, result) {
            if (!!error) {
                logger.error(`update activity in activities failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};

Dao.prototype.deleteById = function (id, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ACTIVITY_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`delete activity in activities failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.findOneAndDelete({id : id}, function (error, result) {
            if (!!error) {
                logger.error(`delete activity in activities failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};


module.exports.create = function (app) {
    return new Dao(app);
};
